package com.example.myandroidapp1;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;

import com.example.myandroidapp1.activity.AbstractBaseActivity;
import com.example.myandroidapp1.client.MyWebViewClient;

@SuppressLint("SetJavaScriptEnabled") 
public class SimpleBrowser extends AbstractBaseActivity implements
		OnClickListener {

	private WebView browser;
	private EditText et;

	private static final String URL = "http://www.sothebys.com";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.browser);
		browser = (WebView) findViewById(R.id.wvBrowser);
		
		browser.getSettings().setJavaScriptEnabled(true);
		browser.getSettings().setAllowFileAccess(true);
	    
		et = (EditText) findViewById(R.id.etBrowserAddress);
		browser.setWebViewClient(new MyWebViewClient());
		browser.loadUrl(URL);

		Button go = (Button) findViewById(R.id.bBrowserGo);
		Button back = (Button) findViewById(R.id.bBrowserBack);
		Button forward = (Button) findViewById(R.id.bBrowserForward);
		Button refresh = (Button) findViewById(R.id.bBrowserRefresh);
		Button clear = (Button) findViewById(R.id.bBrowserClear);

		go.setOnClickListener(this);
		back.setOnClickListener(this);
		forward.setOnClickListener(this);
		refresh.setOnClickListener(this);
		clear.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bBrowserBack:
			if (browser.canGoBack()) {
				browser.goBack();
			}
			break;
		case R.id.bBrowserForward:
			browser.goForward();
			break;

		case R.id.bBrowserRefresh:
			browser.reload();
			break;

		case R.id.bBrowserClear:
			browser.clearHistory();
			break;

		case R.id.bBrowserGo:
			String webUrl = et.getText().toString();
			browser.loadUrl(webUrl);
			InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromWindow(et.getWindowToken(),0);
			break;

		default:
			break;
		}

	}
}
